﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.Bll;

public partial class Templates_HomePage_Default : BaseTemplatePage
{
    protected override void AttachEvents()
    {
        acmsQuote.DataBound += new EventHandler(acmsQuote_DataBound);
        acmsQuoteMaker.DataBound += new EventHandler(acmsQuoteMaker_DataBound);
        ucTestamonialsModule.DataRequired += new EventHandler(ucTestamonialsModule_DataRequired);
        ucExploreModule.DataRequired += new EventHandler(ucExploreModule_DataRequired);
        ucFeedNews.SelectData += new EventHandler<Acms.DataControls.SelectEventArgs<Post>>(ucFeedNews_SelectData);
        ucFeedCases.SelectData += new EventHandler<Acms.DataControls.SelectEventArgs<Post>>(ucFeedCases_SelectData);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataBind();
        }
    }

    void ucFeedCases_SelectData(object sender, Acms.DataControls.SelectEventArgs<Post> e)
    {
        if (AcmsContext.CurrentPost != null)
        {
            e.SetData(GetArticles(Post.GetAllBySiteIdAndParentPath(AcmsContext.SiteId, "~/Cases", AcmsContext.SiteVersion)));
        }
        else
        {
            e.SetData(DummyPost.CreateDummyList(12));
        }
    }

    void ucFeedNews_SelectData(object sender, Acms.DataControls.SelectEventArgs<Post> e)
    {
        if (AcmsContext.CurrentPost != null)
        {
            e.SetData(GetArticles(Post.GetAllBySiteIdAndParentPath(AcmsContext.SiteId, "~/News", AcmsContext.SiteVersion)));
        }
        else
        {
            e.SetData(DummyPost.CreateDummyList(12));
        }
    }


    void ucExploreModule_DataRequired(object sender, EventArgs e)
    {
        if (AcmsContext.CurrentPost != null)
        {
            List<Post> tmp = new List<Post>();
            foreach (Post child in AcmsContext.CurrentPost.Children)
            {
                if (child.Path.ToLower() != "~/testamonials" && child.Path.ToLower() != "~/cases" && child.Path.ToLower() != "~/news" && child.Path.ToLower() != "~/offices" && child.Path.ToLower() != "~/footers")
                {
                    tmp.Add(child);
                }
            }
            ucExploreModule.DataSource = tmp;
        }
        else
        {
            ucExploreModule.DataSource = DummyPost.CreateDummyList(8);
        }
    }

    void ucTestamonialsModule_DataRequired(object sender, EventArgs e)
    {
        ucTestamonialsModule.DataSource = Post.GetAllBySiteIdAndParentPath(AcmsContext.SiteId, "~/Testamonials", AcmsContext.SiteVersion);
    }

    void acmsQuoteMaker_DataBound(object sender, EventArgs e)
    {
        if (acmsQuoteMaker.IsEmpty)
        {
            quoteMakerWrapper.Visible = false;
        }
        else
        {
            quoteMakerWrapper.Visible = true;
        }
    }

    void acmsQuote_DataBound(object sender, EventArgs e)
    {
        if (acmsQuote.IsEmpty)
        {
            quoteWrapper.Visible = false;
        }
    }
}
